package com.brotherly.framework.event;

import java.util.HashMap;
import java.util.Map;

import android.util.SparseArray;

import com.brotherly.framework.service.IService;
import com.brotherly.framework.service.ServiceHost;
import com.brotherly.framework.transaction.TransactionServiceHost;

public class EventService implements IService
{
    public static final String description = "com.brotherly.framework.event.EVENT";

    protected ServiceHost host;

    protected Map<String, SparseArray<EventListener>> registers;

    public EventService(ServiceHost host)
    {
        registers = new HashMap<String, SparseArray<EventListener>>();
        this.host = host;
    }

    @Override
    public void init()
    {

    }

    public void registerLocationEvent(String id, boolean newEvent)
    {}

    public int registerEventListener(String id, EventListener listener)
    {
        if (listener == null)
            return 0;

        boolean newEvent = false;

        Integer hashcode = Integer.valueOf(listener.hashCode());

        SparseArray<EventListener> listeners = null;
        synchronized (registers)
        {
            listeners = registers.get(id);
            if (listeners == null)
            {
                listeners = new SparseArray<EventListener>();
                registers.put(id, listeners);

                newEvent = true;
            }
        }

        synchronized (listeners)
        {
            listeners.put(hashcode, listener);

            registerLocationEvent(id, newEvent);
        }

        return hashcode.intValue();
    }

    public int signEvent(final String id, final Object param1, final Object param2)
    {
        final SparseArray<EventListener> listeners = registers.get(id);
        int count = 0;
        if (listeners == null || (count = listeners.size()) == 0)
            return count;

        TransactionServiceHost.getThreadPool().execute(new Runnable()
        {
            @Override
            public void run()
            {
                SparseArray<EventListener> listeners_copy = new SparseArray<EventListener>(listeners.size());
                synchronized (listeners)
                {
                    for (int i = 0; i < listeners.size(); i++)
                    {
                        int key = listeners.keyAt(i);
                        EventListener listener = listeners.get(key);
                        listeners_copy.put(key, listener);

                    }
                }

                for (int i = 0; i < listeners_copy.size(); i++)
                {
                    int key = listeners.keyAt(i);
                    EventListener listener = listeners_copy.get(key);
                    listener.onEvent(id, param1, param2);
                }

            }
        });

        return count;
    }

    public void unregisterLocationEvent(String id, boolean removeEvent)
    {

    }

    public void unregisterEventListener(String id, int stub)
    {
        SparseArray<EventListener> listeners = registers.get(id);
        synchronized (registers)
        {
            listeners = registers.get(id);
        }

        if (listeners != null)
        {
            boolean removeEvent = false;

            synchronized (listeners)
            {
                listeners.remove(Integer.valueOf(stub));
                if (listeners.size() == 0)
                {
                    synchronized (registers)
                    {
                        registers.remove(id);
                        removeEvent = true;
                    }
                }
                unregisterLocationEvent(id, removeEvent);
            }
        }
    }
}
